﻿using AutoMapper;
using B.S.BasicData.Api.Read.Application.Command.User;
using B.S.BasicData.Api.Read.DTOS;
using B.S.BasicData.Domian.Entitys;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infastructure.Interface;
using MediatR;

namespace B.S.BasicData.Api.Read.Application.Handler.User
{
    public class GetCountyHandler : IRequestHandler<GetCountyCommand, ApiResult<List<CountyDto>>>
    {
        private readonly IBaseRepository<CountyModel> _countyRepository;
        private readonly IMapper _mapper;

        public GetCountyHandler(IBaseRepository<CountyModel> countyRepository, IMapper mapper)
        {
            _countyRepository = countyRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取县级下拉框
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<List<CountyDto>>> Handle(GetCountyCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<CountyDto>> result = new ApiResult<List<CountyDto>>();

            var county =_countyRepository.GetAll().Where(x=>x.CityId==request.CityId).ToList();
            var model = _mapper.Map<List<CountyDto>>(county);
            result.Code =StatusEnum.OK;
            result.Data = model;
            result.Message = "获取列表成功";
            return Task.FromResult(result);
        }
    }
}
